package com.huayue.controller;

import java.math.BigDecimal;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.support.spring.FastJsonJsonView;
import com.huayue.annotation.LogAnno;
import com.huayue.dao.AliPayDao;
import com.huayue.entity.Customer;
import com.huayue.entity.RoomPredictOrder;
import com.huayue.entity.RoomPredictOrderDetails;
import com.huayue.entity.User;
import com.huayue.service.CustomerService;
import com.huayue.service.RoomPredictOrderService;
import com.huayue.service.RoomService;
import com.huayue.service.RoomTypeService;
import com.huayue.util.DateHelper;
import com.huayue.util.OrderNumberHelper;
import com.huayue.util.PageHelper;

/**
 * 预定信息
 */
@Controller
@RequestMapping("room_predict_order")
public class RoomPredictOrderController {

    @Autowired
    RoomPredictOrderService svc;
    @Autowired
    CustomerService comsvc;
    @Autowired
    RoomTypeService rtsvc;

    @Autowired
    RoomService rsvc;

    BaseController bc = new BaseController();

    @RequestMapping("/toAdd")
    public ModelAndView toAdd(RoomPredictOrder roomPredictOrder, String checkDate, Integer days) {
        FastJsonJsonView view = new FastJsonJsonView();
        // 根据前台判断是否需要生成订单号
        if ("true".equals(roomPredictOrder.getOrderNumber())) {
            view.addStaticAttribute("orderNumber", OrderNumberHelper.getOrderNumber(OrderNumberHelper.YDDORDER, "YDD"));
        }
        view.addStaticAttribute("roomType", rtsvc.findAll());
        view.addStaticAttribute("room", svc.findNullfrom(checkDate, days));
        return new ModelAndView(view);
    }

    @LogAnno(operateType = "添加-预定房间")
    @RequestMapping("/add")
    public ModelAndView add(@RequestBody String json, HttpServletRequest request) {
        // 获取session中该用户信息
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute("USER");
        // 格式化json数据
        JSONObject jsonData = JSONObject.parseObject(json);
        // 创建订单信息
        RoomPredictOrder order = new RoomPredictOrder();
        order.setOrderNumber(String.valueOf(jsonData.get("orderNumber")));
        order.setPredictInTime(DateHelper.StringToDateShort(jsonData.get("realInTime").toString()));
        order.setPredictOutTime(DateHelper.StringToDateShort(jsonData.get("realOutTime").toString()));
        order.setRemark(jsonData.get("remark").toString());
        order.setTotalPrice(new BigDecimal(jsonData.get("totalPay").toString()));
        Customer cus = new Customer();
        String sex = jsonData.get("sex").toString();
        if ("男".equals(sex)) {
            cus.setSex("0");
        } else {
            cus.setSex("1");
        }
        cus.setPhone(jsonData.get("phone").toString());
        cus.setIdnumber(jsonData.get("idNumber").toString());
        cus.setRealname(jsonData.get("linkman").toString());
        JSONArray jsonArray = jsonData.getJSONArray("table");
        // 获取订单详情
        List<RoomPredictOrderDetails> details =
            JSONArray.parseArray(jsonArray.toString(), RoomPredictOrderDetails.class);
        // 将订单信息和订单详情传至service进行处理
        boolean result = svc.addDetails(order, details, cus);
        // 将临时订单号销毁
        OrderNumberHelper.removeOrderNumber(OrderNumberHelper.YDDORDER, order.getOrderNumber());
        // 返回数据给页面
        FastJsonJsonView view = new FastJsonJsonView();
        view.addStaticAttribute("code", 0);
        if (result) {
            view.addStaticAttribute("icon", 1);
            view.addStaticAttribute("msg", "订单添加成功！");
        } else {
            view.addStaticAttribute("icon", 2);
            view.addStaticAttribute("msg", "订单添加失败！");
        }
        return new ModelAndView(view);
    }

    /**
     * 清除订单号
     *
     * @param room_predict_order
     * @return
     */
    @RequestMapping("/close")
    public ModelAndView close(RoomPredictOrder room_predict_order) {
        // 获取 审核人信息 商品信息 供货商信息
        FastJsonJsonView view = new FastJsonJsonView();
        OrderNumberHelper.removeOrderNumber(OrderNumberHelper.YDDORDER, room_predict_order.getOrderNumber());
        view.addStaticAttribute("code", 0);
        return new ModelAndView(view);
    }

    // 这个是查看详情要用的
    @RequestMapping("/findDetails")
    public ModelAndView findDetails(String id) {
        FastJsonJsonView view = new FastJsonJsonView();
        view.addStaticAttribute("code", 0);
        RoomPredictOrder order = new RoomPredictOrder();
        order.setId(Long.valueOf(id));
        // 所有房间信息
        view.addStaticAttribute("room", rsvc.findAllRoomAndType());
        // 先找到一个
        view.addStaticAttribute("order", svc.findOne(order));
        // 然后传数据
        view.addStaticAttribute("data", svc.findDetails(id));
        return new ModelAndView(view);
    }

    @RequestMapping("/delete")
    public ModelAndView delete(RoomPredictOrder room_predict_order) {
        return bc.delete(svc, room_predict_order);
    }

    @RequestMapping("/update")
    public ModelAndView update(RoomPredictOrder room_predict_order) {
        return bc.update(svc, room_predict_order);
    }

    @RequestMapping("/findOne")
    public ModelAndView findOne(RoomPredictOrder room_predict_order) {
        return bc.findOne(svc, room_predict_order);
    }

    @RequestMapping("/findPage")
    public ModelAndView findPage(PageHelper page, RoomPredictOrder room_predict_order) {
        return bc.findPage(svc, page, room_predict_order, room_predict_order);
    }

    @RequestMapping("/payFind")
    public ModelAndView payFind(RoomPredictOrder order) {
        FastJsonJsonView view = new FastJsonJsonView();
        view.addStaticAttribute("code", 0);
        AliPayDao aliPayDao = new AliPayDao();
        int i = aliPayDao.findPayByOrderNumber(order.getOrderNumber()).size();
        if (i == 1) {
            view.addStaticAttribute("msg", "已付款");
        } else {
            view.addStaticAttribute("msg", "未付款");
        }
        return new ModelAndView(view);
    }

    @RequestMapping("/toArrival")
    public ModelAndView toArrival(String id) {
        FastJsonJsonView view = new FastJsonJsonView();
        // 查询当前房间的预约信息 根据房间id查询该房间的预定订单信息
        view.addStaticAttribute("code", 0);
        view.addStaticAttribute("info", svc.findPredictByRoomId(id));
        return new ModelAndView(view);
    }
}